package com.dream.web.controller;

import com.dream.dto.UserDTO;
import com.dream.service.UserService;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by IntelliJ IDEA.
 * User: ZhongGang
 * Date: 12-12-22
 * Time: 下午7:47
 */
public class UserRegistrationFormController extends SimpleFormController {
    private UserService userService;

    public UserRegistrationFormController() {
        setCommandClass(UserDTO.class);
        setCommandName("user");
        setFormView("user/registrationForm");
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        return new UserDTO();
    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
        UserDTO userDTO = (UserDTO) command;
        userService.saveUser(userDTO);
        return null;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
